package com.cmc.cloud.cmclink.doc.service.impl.si;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.BlnoFreightErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.BlnoFreightForecastConvert;
import com.cmc.cloud.cmclink.doc.entity.BlnoFreightForecastDO;
import com.cmc.cloud.cmclink.doc.mapper.BlnoFreightForecastMapper;
import com.cmc.cloud.cmclink.doc.service.BlnoFreightForecastService;
import com.cmc.cloud.cmclink.doc.vo.blnofreightvo.BlnoFreightForecastCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnofreightvo.BlnoFreightForecastUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 海运费附加费预计算 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class BlnoFreightForecastServiceImpl implements BlnoFreightForecastService {

    @Resource
    private BlnoFreightForecastMapper blnoFreightForecastMapper;

    @Override
    public void createBlnoFreightForecast(BlnoFreightForecastCreateReqVO createReqVO) {
        // 插入
        BlnoFreightForecastDO blnoFreightForecast = BlnoFreightForecastConvert.INSTANCE.convert(createReqVO);
        blnoFreightForecastMapper.insert(blnoFreightForecast);
    }

    @Override
    public void updateBlnoFreightForecast(BlnoFreightForecastUpdateReqVO updateReqVO) {
        // 校验存在
        validateBlnoFreightForecastExists(updateReqVO.getId());
        // 更新
        BlnoFreightForecastDO updateObj = BlnoFreightForecastConvert.INSTANCE.convert(updateReqVO);
        blnoFreightForecastMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlnoFreightForecast(Long id) {
        // 校验存在
        validateBlnoFreightForecastExists(id);
        // 删除
        blnoFreightForecastMapper.deleteById(id);
    }

    private void validateBlnoFreightForecastExists(Long id) {
        if (blnoFreightForecastMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(BlnoFreightErrorCodeConstants.BLNO_FREIGHT_FORECAST_NOT_EXISTS);
        }
    }

    @Override
    public BlnoFreightForecastDO getBlnoFreightForecast(Long id) {
        return blnoFreightForecastMapper.selectById(id);
    }

    @Override
    public List<BlnoFreightForecastDO> getBlnoFreightForecastByBlonId(Long blnoId) {
        LambdaQueryWrapperX<BlnoFreightForecastDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(BlnoFreightForecastDO::getBlnoId, blnoId);
        return blnoFreightForecastMapper.selectList(queryWrapperX);
    }

    @Override
    public List<BlnoFreightForecastDO> getBlnoFreightForecastList(Collection<Long> ids) {
        return blnoFreightForecastMapper.selectBatchIds(ids);
    }

    @Override
    public void deleteBlnoFreightForecast(Collection<Long> ids) {
        LambdaQueryWrapperX<BlnoFreightForecastDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.in(BlnoFreightForecastDO::getBlnoId, ids);
        blnoFreightForecastMapper.delete(queryWrapperX);
    }

    @Override
    public void insertBatchBlnoFreightForecast(List<BlnoFreightForecastDO> blnoFreightDOList) {
        blnoFreightForecastMapper.insertBatch(blnoFreightDOList);
    }
}
